<?php
/**
 * @author Pham Van Diep
 * @copyright 2014
 * @description: save sponsor
 */
// not login
// not perm add
if(!isset($AppUI->id) ||
	!isset($_SESSION['sponsor']) ||
	(isset($_SESSION['sponsor']) && ($_SESSION['sponsor']&1<<1) == 0) ||
    !isset($_POST['clubid'])

) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
// data input
//var_dump($_POST); die();
$arrErr = array();
$intClubId = intval($_POST['clubid']);

$strName            = CI_Function::stripTag(trim($this->input->post('name',null))); 
$intLen = strlen($strName);
if ($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['name_range']);
}
$strShort    	    = CI_Function::stripTag(trim($this->input->post('sshort',null)));
$intLen = strlen($strShort);
if ($intLen < VL_SHORT_MINLENGTH || $intLen > VL_SHORT_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['short_range']);
}			
$strDes             = CI_Function::stripTag(trim($this->input->post('description',null)));
$intLen = strlen($strDes);
if ($intLen < VL_CONTENT_MINLENGTH ) {
    array_push($arrErr, $arrLocale['description_minlength']);
}
$strTag             = CI_Function::stripTag(trim($this->input->post('tag', null)));
$link = CI_Function::stripTag(trim($this->input->post('link',null)));
if ($link == null) {
    if($_FILES['logo']['error'] > 0) {
        array_push($arrErr, $arrLocale['logo_upload']);
    }else{
        $this->load->library('upload');
        $arrInfo = $this->upload->do_upload('logo');
    }
}else{
    $this->load->library('upload');
    $arrInfo = $this->upload->do_upload_url($link);
}
if(!isset($arrInfo) || count($arrInfo) == 0) {
    array_push($arrErr, $arrLocale['logo_upload']);
}
if(count($arrErr)==0) {//allow save          
    $strLogo = CI_Function::generate($arrInfo['path'].$arrInfo['firstname'].'.'.$arrInfo['extension'], IMAGE_LOGO_SIZE,IMAGE_LOGO_SIZE);
    if(isset($_SESSION['SPONSOR_ERROR'])) unset($_SESSION['SPONSOR_ERROR']);
	if(isset($_SESSION['SPONSOR_INPUT'])) unset($_SESSION['SPONSOR_INPUT']);
   // $strLogo = CI_Function::getRootImageUrl($arrInfo['path'],$arrInfo['firstname'],$arrInfo['extension']);          
    $intError = $this->sponsor->add($AppUI->id,$intClubId,$strName, CI_Function::getUrl($strLogo), $strShort,$strDes, $strTag);
	if($intError == 0) CI_Function::setMsg($arrLocale['success']);    
    else if($intError == -1) array_push($arrErr,$arrLocale['system_error']);        
    else{
        array_push($arrErr,$arrError['problem_save_db_error']);        
    }          
}
// occur error

if (count($arrErr) != 0) {             
    $_SESSION['SPONSOR_ERROR'] = $arrErr;
    $_SESSION['SPONSOR_INPUT'] = $this->input->post();
	redirect(HOST_BACKEND . 'sponsor/add?clubid='.$intClubId);
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);

